<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;

use App\Http\Model\RedisModel;

class WechatModel extends Model
{
    /*
        获取微信配置信息
        @return array
    */
    private static function getConf()
    {
        $Conf = \Config::get("wechat");
        return $Conf;
    }

    /*
        获取AccessToken
        @return string
            "q5kiyLVbnzi53ESKUUt9D_b4hYDnNTRw6Ncjdse2Dh904AZFLnXXJ4mkL5KQatLaOUHd8HKG64zsVBnUXmOaI948CyLohFi99D8gfj-9uH4UWYfHYo7iCZEovyMLVZc7OBPcAHASFT"
    */
    public static function getAccessToken()
    {
        $accessToken = self::getAccessTokenByRedis();

        if(false == empty($accessToken)){
            return $accessToken;
        }else{
            $Conf = self::getConf();

            $accessToken = self::reqAccessToken($Conf['app_key'], $Conf['app_secret']);
            if(false == $accessToken) return false;

            self::setAccessTokenToRedis($accessToken['access_token'], $accessToken['expires_in']);
            $accessToken = $accessToken['access_token'];
        }
        return $accessToken;
    }

    /*
        请求accesstoken
        @return array(
            "access_token" => "q5kiyLVbnzi53ESKUUt9D_b4hYDnNTRw6Ncjdse2Dh904AZFLnXXJ4mkL5KQatLaOUHd8HKG64zsVBnUXmOaI948CyLohFi99D8gfj-9uH4UWYfHYo7iCZEovyMLVZc7OBPcAHASFT",
            "expires_in" => 7200
        )
    */
    private static function reqAccessToken($app_key = '', $app_secret = '')
    {
        $reqUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$app_key.'&secret='.$app_secret;
        $resData = self::Curl($reqUrl);
        return false == empty($resData['access_token'])? $resData : false;
    }

    /*
        从redis获取AccessToken
        @return string 
            "q5kiyLVbnzi53ESKUUt9D_b4hYDnNTRw6Ncjdse2Dh904AZFLnXXJ4mkL5KQatLaOUHd8HKG64zsVBnUXmOaI948CyLohFi99D8gfj-9uH4UWYfHYo7iCZEovyMLVZc7OBPcAHASFT"
    */
    public static function getAccessTokenByRedis()
    {
        $Redis = new RedisModel();
        $accessToken = $Redis->get("mp_wechat_access_token");
        return false == empty($accessToken) ? $accessToken : false;
    }

    /*
        将AccessToken存入redis
        @return bool
            true
    */
    public static function setAccessTokenToRedis($access_token = '', $expire_time = 0)
    {
        $Redis = new RedisModel();
        $Redis->set("mp_wechat_access_token", $access_token);
        $Redis->expire("mp_wechat_access_token", $expire_time);
        return true;
    }

    /*
        Curl_Json_Array
    */
    public static function Curl($url, $data = false){
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt ($ch,  CURLOPT_NOSIGNAL,true);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT_MS,500);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        
        if($data != false){
            curl_setopt($ch,CURLOPT_POST,true);
            curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
        }
        $strRes=curl_exec($ch);
        curl_close($ch);
        $arrResponse=json_decode($strRes,true);
        
        if(is_array($arrResponse) == false){
            return false;
        }
        
        return $arrResponse;
    }
}
